package com.aaron.gesturehelper;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.Style;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class PictureEditView extends View {

	private static final int TOUCH_IN_RECT = 0;
	private static final int TOUCH_OUT_RECT = 1;
	private static final int TOUCH_ON_POINT = 2;
	
	private int mTouchMode;
	
	private int[] mWindowPos = new int[2];
	private Rect mClipRect = new Rect();
	private Paint mPaint = new Paint();
	
	private long mQuitTimeout = 500;
	private long mPressTime = 0 , mLastPressTime = 0;
	
	private Handler mHandler;
	
	private int mScreenWidth,mScreenHeight;
	private int mCurrentTouchX,mCurrentTouchY,mPressX,mPressY;
	
	private boolean mInit;
	
	public PictureEditView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mPaint.setColor(Color.GREEN);
		mPaint.setStrokeWidth(2);
		mPaint.setAntiAlias(true);
	}

	public void onDraw(Canvas c) {
		if(!mInit) {
			getLocationOnScreen(mWindowPos);
			mInit = true;
		}
		mPaint.setStyle(Style.STROKE);
		c.drawRect(mClipRect, mPaint);
		mPaint.setStyle(Style.FILL);
		c.drawCircle(mClipRect.right, mClipRect.bottom, 5, mPaint);
	}
	
	public boolean onTouchEvent(MotionEvent e) {
		int action = e.getAction();
		int x = (int) e.getX();
		int y = (int) e.getY();
		
		switch(action) {
		case MotionEvent.ACTION_DOWN:
			mTouchMode = getTouchMode(x, y);
			mLastPressTime = mPressTime;
			mPressTime = System.currentTimeMillis();
			mPressX = x;
			mPressY = y;
			break;
		case MotionEvent.ACTION_UP:
			if(mTouchMode == TOUCH_IN_RECT) {
				mClipRect.offset(x-mCurrentTouchX, y-mCurrentTouchY);
				invalidate();
			} else if(mTouchMode == TOUCH_ON_POINT) {
				mClipRect.right += x-mCurrentTouchX;
				mClipRect.bottom += y-mCurrentTouchY;
				if(mClipRect.right < mClipRect.left) {
					int tmp = mClipRect.left;
					mClipRect.left = mClipRect.right;
					mClipRect.right = tmp;
				}
				if(mClipRect.bottom < mClipRect.top) {
					int tmp = mClipRect.top;
					mClipRect.top = mClipRect.bottom;
					mClipRect.bottom = tmp;
				}
				invalidate();
			}
			if(System.currentTimeMillis() - mLastPressTime < mQuitTimeout) {
				mHandler.sendEmptyMessage(MyViewManager.MSG_PICTURE_EDIT_FINISHED);
			} else if(mTouchMode == TOUCH_OUT_RECT && System.currentTimeMillis() - mPressTime < mQuitTimeout && mPressX - x > 150 && Math.abs(y-mPressY) < 50 ) {
				mClipRect.setEmpty();
				mHandler.sendEmptyMessage(MyViewManager.MSG_PICTURE_EDIT_FINISHED);
			}
			break;
		case MotionEvent.ACTION_MOVE:
			if(mTouchMode == TOUCH_IN_RECT) {
				mClipRect.offset(x-mCurrentTouchX, y-mCurrentTouchY);
				invalidate();
			} else if(mTouchMode == TOUCH_ON_POINT) {
				mClipRect.right += x-mCurrentTouchX;
				mClipRect.bottom += y-mCurrentTouchY;
				invalidate();
			}
			break;
		}
		mCurrentTouchX = x;
		mCurrentTouchY = y;
		return true;
	}
	
	public void setHandler(Handler handler) {
		mHandler = handler;
	}
	
	public void initShowing(MyViewManager manager) {
		mInit = false;
		mScreenWidth = manager.mScreenWidth;
		mScreenHeight = manager.mScreenHeight;
		mClipRect.set(mScreenWidth/2-75, mScreenHeight/2-100, mScreenWidth/2+75, mScreenHeight/2);
	}
	
	private int getTouchMode(int x, int y) {
		if(mClipRect.right-15 < x && x < mClipRect.right+15
				&& mClipRect.bottom-15 < y && y < mClipRect.bottom+15) {
			return TOUCH_ON_POINT;
		} else if(mClipRect.contains(x, y)) {
			return TOUCH_IN_RECT;
		} else {
			return TOUCH_OUT_RECT;
		}
	}
	
	public void changeViewSize(int width, int height) {
		mScreenWidth = width;
		mScreenHeight = height;
	}
	
	public Rect getClipRect() {
		if(mClipRect.isEmpty()) {
			return mClipRect;
		}
		
		int left = mClipRect.left < 0 ? 0 : mClipRect.left;
		int top = mClipRect.top < 0 ? 0 : mClipRect.top;
		top += mWindowPos[1];
		int right = mClipRect.right > mScreenWidth ? mScreenWidth : mClipRect.right;
		int bottom = mClipRect.bottom+mWindowPos[1] > mScreenHeight? mScreenHeight :mClipRect.bottom+mWindowPos[1];
		mClipRect.set(left, top, right, bottom);
		return mClipRect;
	}
}
